﻿// This sample code has been built using EMC Documentum Foundation Services (DFS)
// version 6.5 SP1, Visual Studio 2008 and .NET 3.5 (Windows Communication Foundation
// "v2" and LINQ to XML--System.Xml.Linq). It is not supported by EMC.
//
// In order to build and run this sample you must have DFS 6.5 SP1 (i.e. its web 
// services) properly deployed (e.g. into a JEE application server such as JBoss 
// 4.2.2).
//
// The primary intent of this sample code is to demonstrate direct-to-WSDL consumption
// of DFS using standard Microsoft tools (Visual Studio) and frameworks (WCF).
//
// Please visit the EMC Developer Network for more on DFS:
//     https://community.emc.com/community/edn/documentum
using System.Collections.Generic;
using System.Globalization;
using System.ServiceModel.Channels;
using System.Xml;
using System.Xml.Serialization;

using System.IO;
using System.Xml.Linq;

using D6.DFS.Consumer.ContextRegistryService;

namespace D6.DFS.Consumer
{
    public class ServiceContextHeader : MessageHeader
    {
        private readonly List<RepositoryIdentity> repositoryIdentities = new List<RepositoryIdentity>();
        private ContentTransferProfile contentTransferProfile;

        public void AddRepositoryIdentity(RepositoryIdentity rid)
        {
            if (rid == null)
                return;

            repositoryIdentities.Add(rid);
        }

        public void AddContentTransferProfile(ContentTransferProfile ctp)
        {
            if (ctp == null)
                return;

            contentTransferProfile = ctp;
        }

        protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
        {
            // If there are no ids, do nothing
            if (repositoryIdentities.Count < 1)
                return;

            // Add ids to the ServiceContext
            ServiceContext serviceContext = new ServiceContext();
            serviceContext.Identities = new Identity[repositoryIdentities.Count];
            for (int x = 0; x < repositoryIdentities.Count; x++)
                serviceContext.Identities[x] = repositoryIdentities[x];

            // If there is a ContentTransferProfile, add it
            if (contentTransferProfile != null)
            {
                serviceContext.Profiles = new ContentTransferProfile[1];
                serviceContext.Profiles[0] = contentTransferProfile;
            }

            // Serialize the objects to XML
            XDocument doc = null;
            using (StringWriter sw = new StringWriter(CultureInfo.CurrentCulture))
            {
                XmlSerializer xs = new XmlSerializer(typeof(ServiceContext));
                xs.Serialize(sw, serviceContext);
                doc = XDocument.Load(new StringReader(sw.ToString()));
            }

            // Add Identities to the ServiceContext header
            foreach (XElement el in doc.Descendants(XName.Get("Identities", "http://context.core.datamodel.fs.documentum.emc.com/")))
            {
                XmlReader r = XmlReader.Create(new StringReader(el.ToString()));
                writer.WriteNode(r, true);
            }

            // Add Profiles to the ServiceContext header 
            if (serviceContext.Profiles != null)
            {
                foreach (XElement el in doc.Descendants(XName.Get("Profiles", "http://context.core.datamodel.fs.documentum.emc.com/")))
                {
                    XmlReader r = XmlReader.Create(new StringReader(el.ToString()));
                    writer.WriteNode(r, false);
                }
            }
        }

        public override string Name
        {
            get { return "ServiceContext"; }
        }

        public override string Namespace
        {
            get { return "http://context.core.datamodel.fs.documentum.emc.com/"; }
        }
    }
}